
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/**
 * 洛阳皇宫
 */
public class LuoyangImperialPalace : MonoBehaviour
{

    public GameObject[] item;
    private List<Vector3> itemPositionList = new List<Vector3>();






    private void Awake()
    {
        // // 实例化老家                              无旋转
        // CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);

        // // 用墙把老家围起来
        // CreateItem(item[2], new Vector3(-1, -8, 0), Quaternion.identity);
        // CreateItem(item[2], new Vector3(1, -8, 0), Quaternion.identity);
        // for (int i = -1; i < 2; i++)
        // {
        //     CreateItem(item[2], new Vector3(i, -7, 0), Quaternion.identity);
        // }

        //实例外围墙
        for (int i = 0; i <= 12; i++)
        {
            CreateItem(item[2], new Vector3(i, 0, 0), Quaternion.identity);
            if (i < 5)
            {
                CreateItem(item[2], new Vector3(i, -15, 0), Quaternion.identity);
            }
            if (i > 8)
            {
                CreateItem(item[2], new Vector3(i, -15, 0), Quaternion.identity);
            }
        }



        for (int i = -20; i < 0; i++)
        {
            CreateItem(item[2], new Vector3(0, i, 0), Quaternion.identity);
            CreateItem(item[2], new Vector3(19, i, 0), Quaternion.identity);
        }
        // // 实例化地图  //0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
        // for (int i = 0; i < 80; i++)
        // {
        //     // 墙
        //     CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
        // }
        // for (int i = 0; i < 20; i++)
        // {
        //     // 障碍
        //     CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
        // }
        // for (int i = 0; i < 10; i++)
        // {
        //     // 河流
        //     CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
        // }
        // for (int i = 0; i < 20; i++)
        // {
        //     // 草
        //     CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
        // }
    }
    /**
     * 创建物品
     */
    private void CreateItem(GameObject createCameObject, Vector3 createPosition, Quaternion createRotation)
    {
        GameObject itemGo = Instantiate(createCameObject, createPosition, createRotation);
        itemGo.transform.SetParent(gameObject.transform);
        itemPositionList.Add(createPosition);
    }

    /**
     * 随机生成坐标
     */
    private Vector3 CreateRandomPosition()
    {
        while (true)
        {
            Vector3 cratePosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
            if (!HasThePosition(cratePosition))
            {
                return cratePosition;
            }
        }
    }


    /**
     * 判断当前坐标是否已经存在
     */
    private bool HasThePosition(Vector3 createPos)
    {
        for (int i = 0; i < itemPositionList.Count; i++)
        {
            if (createPos == itemPositionList[i])
            {
                return true;
            }
        }
        return false;
    }
}
